/* Copyright 2015 David Overeem (dovereem@cox.net)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.xtce.apps.editor.ui;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import org.omg.space.xtce.HeaderType;
import org.omg.space.xtce.HeaderType.NoteSet;
import org.xtce.toolkit.XTCESpaceSystem;

/**
 *
 * @author David Overeem
 *
 */

public class XTCEViewerNoteSet extends javax.swing.JPanel {

    /**
     * Creates new form XTCEViewerNoteSet
     */
    public XTCEViewerNoteSet() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addNoteButton = new javax.swing.JButton();
        noteListLabel = new javax.swing.JLabel();
        noteParentPanel = new javax.swing.JPanel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/xtce/toolkit/MessagesBundle"); // NOI18N
        addNoteButton.setText(bundle.getString("tab_ssdetail_addnote_text")); // NOI18N
        addNoteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNoteButtonActionPerformed(evt);
            }
        });

        noteListLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        noteListLabel.setText(bundle.getString("tab_ssdetail_notelist_text")); // NOI18N

        javax.swing.GroupLayout noteParentPanelLayout = new javax.swing.GroupLayout(noteParentPanel);
        noteParentPanel.setLayout(noteParentPanelLayout);
        noteParentPanelLayout.setHorizontalGroup(
            noteParentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        noteParentPanelLayout.setVerticalGroup(
            noteParentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 141, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(noteParentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 255, Short.MAX_VALUE)
                        .addComponent(addNoteButton)
                        .addGap(0, 255, Short.MAX_VALUE))
                    .addComponent(noteListLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(noteListLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(noteParentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addNoteButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addNoteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNoteButtonActionPerformed
        addNote( "New Note" );
    }//GEN-LAST:event_addNoteButtonActionPerformed

    public void setSpaceSystem( XTCESpaceSystem spaceSystem, XTCEViewerSpaceSystemDetails parentScrollPane ) {

        spaceSystem_      = spaceSystem;
        parentScrollPane_ = parentScrollPane;

        HeaderType header = spaceSystem_.getReference().getHeader();
        if ( header != null ) {
            NoteSet notes = header.getNoteSet();
            if ( notes != null ) {
                noteItemText_.clear();
                noteItemText_.addAll( notes.getNote() );
                makeContentPanel( noteItemText_ );
            }
        }

    }

    public void setEditable( boolean editEnabled ) {
        addNoteButton.setEnabled( editEnabled );
        Component[] components = noteParentPanel.getComponents();
        for ( Component component : components ) {
            if ( component.getClass() == JPanel.class ) {
                Component[] innerComponents = ((JPanel)component).getComponents();
                for ( Component innerComponent : innerComponents ) {
                    if ( innerComponent.getClass() == XTCEViewerNote.class ) {
                        ((XTCEViewerNote)innerComponent).setEditable( editEnabled );
                    }
                }
            }
        }
    }

    public void removeNote( int idx ) {
        noteItemText_.remove( idx );
        makeContentPanel( noteItemText_ );
        updateDocument();
    }
    
    public void addNote( String newNoteText ) {
        noteItemText_.add( newNoteText );
        makeContentPanel( noteItemText_ );
        updateDocument();
    }

    public void editNote( String newNoteText, int idx ) {
        noteItemText_.set( idx, newNoteText );
        updateDocument();
    }

    private void makeContentPanel( List<String> notes ) {

        JPanel notePanel = new JPanel();
        notePanel.setLayout( new BoxLayout( notePanel, BoxLayout.Y_AXIS ) );

        int idx = 0;

        for ( String note : notes ) {
            XTCEViewerNote noteEntry = new XTCEViewerNote( this, note, idx++ );
            notePanel.add( noteEntry );
        }

        noteParentPanel.removeAll();
        noteParentPanel.setLayout( new BoxLayout( noteParentPanel,
                                                  BoxLayout.Y_AXIS ) );
        noteParentPanel.add( notePanel );
        noteParentPanel.revalidate();
        noteParentPanel.repaint();

        parentScrollPane_.sizeChanged();

    }

    private void updateDocument() {

        // ensure that the Header element exists
        HeaderType header = spaceSystem_.getReference().getHeader();
        if ( header == null ) {
            header = new HeaderType();
            spaceSystem_.getReference().setHeader( header );
        }

        // first clear the NoteSet element if there are no notes
        NoteSet notes = header.getNoteSet();
        if ( noteItemText_.isEmpty() == true ) {
            if ( notes != null ) {
                header.setNoteSet( null );
            }
            return;
        }

        // add a Note element for each one
        if ( notes == null ) {
            notes = new NoteSet();
            header.setNoteSet( notes );
        }
        notes.getNote().clear();
        notes.getNote().addAll( noteItemText_ );

    }

    // Private Data Members

    private XTCESpaceSystem   spaceSystem_      = null;
    private XTCEViewerSpaceSystemDetails            parentScrollPane_ = null;
    private ArrayList<String> noteItemText_     = new ArrayList<>();


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNoteButton;
    private javax.swing.JLabel noteListLabel;
    private javax.swing.JPanel noteParentPanel;
    // End of variables declaration//GEN-END:variables
}
